package com.unism.data.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParameterValidate {
	private Object object;
	private List<String> errors;
	private boolean flag = false;
	private boolean isNull;
	private boolean isInt;
	
	
	public ParameterValidate(Object object) {
		super();
		errors = new ArrayList<String>();
		isNull = null == object;
		isInt = (object instanceof Integer);
		this.object = object;
	} 
	
	private void addNoRepeatStr(String string) {
		if (!errors.contains(string)) {
			errors.add(string);
		}
	}
	
	/**
	 * 
	 */
	public ParameterValidate notNull() {
		if (null == object) {
			addNoRepeatStr("值不能为空！");
			flag = true;
		}
		return this ; 
	}
	
	
	/**
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public ParameterValidate Size(Integer min, Integer max) {
		if (isNull || !isInt) {
			return this;
		}
		if (null != min && (int)object < min) {
			addNoRepeatStr("对象不能小于"+min+"！");
			flag = true;
		}
		if (null != max && (int)object > max) {
			addNoRepeatStr("对象不能大于"+max+"！");
			flag = true;
		}
		return this;
	}
	/**
	 * 
	 * @param min
	 * @return
	 */
	public ParameterValidate Min(int min) {
		if (isNull || !isInt) {
			return this;
		}
		if (object instanceof Integer) {
			if ( (int)object < min ) {
				addNoRepeatStr("值不能小于"+min+"！");
				flag = true;
			}
		}
		return this;
	}
	
	/**
	 * 
	 * @param max
	 * @return
	 */
	public ParameterValidate Max(int max) {
		if (isNull || !isInt) {
			return this;
		}
		if (object instanceof Integer) {
			if ( (int)object > max ) {
				addNoRepeatStr("值不能大于"+max+"！");
				flag = true;
			}
		}
		return this;
	}
	
	/**
	 * 
	 * @param patternStr
	 * @return
	 */
	public ParameterValidate parttern(String patternStr) {
		if (isNull || isInt) {
			return this;
		}
	      Pattern pattern = Pattern.compile(patternStr);
	      Matcher matcher = pattern.matcher((String)object);
	      if (!matcher.matches()) {
	    	  addNoRepeatStr("该对象不符合正则表达式的规则！ ");
	    	  flag = true;
	      } 
		return this;
	}
	
	public ParameterValidate length(Integer minLength,Integer maxLength) {
		if (isNull || isInt) {
			return this;
		}
		if (object instanceof String) {
			int len = ((String)object).length();
			if (null != minLength && len < minLength) {
				addNoRepeatStr("对象长度不能小于"+minLength+"！");
				flag = true;
			}
			if (null != maxLength && len>maxLength) {
				addNoRepeatStr("对象长度不能大于"+maxLength+"！");
				flag = true;
			}
		}
		return this;
	}
	/**
	 * 
	 * @return
	 */
	public boolean haveError() {
		return flag;
	}
	
	public String getErrors(){
		StringBuffer str = new StringBuffer();
		for(int i = 0; i<errors.size(); i++){
			str.append((i+1)+"、").append(errors.get(i)).append("\r\n");
		}
		return str.toString();
	}
	public static void main(String[] args) {
//		System.out.println(1111);
//		ParameterValidate parameterValidate = new ParameterValidate("0123141");
//		if (parameterValidate.length(3, null).parttern("[1-9]\\d*").haveError()) {
//			System.out.println(parameterValidate.getErrors());
//		}
//		System.out.println(2222);
//		ParameterValidate parameterValidateInt = new ParameterValidate(131445);
//		if (parameterValidateInt.Min(50).Max(1000).haveError()) {
//			System.out.println(parameterValidateInt.getErrors());
//		}
//		System.out.println(3333);
//		ParameterValidate parameterValidateNull = new ParameterValidate(null);
//		if (parameterValidateNull.Min(50).Max(1000).haveError()) {
//			System.out.println(parameterValidateNull.getErrors());
//		}
		List<Long> list  = new ArrayList<>();
		list.add(111L);
		list.add(111L);
		list.add(111L);
		list.add(111L);
		list.add(111L);
		System.out.println(list);
	}
}
